package com.eric.downloader;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class SolrDownloader extends SimplePageDownloader {

	String index, textResult;

	JSONObject jsonResult;

	JSONArray docArray, kwArray;

	HashSet<String> tier2;

	int startN = 0;

	int rows = 20;

	static int numFound = 0;

	String queryBase;

	public SolrDownloader(String indexName) {
		this.index = indexName;
	}

	public static void main(String args[]) {
		SolrDownloader sd = new SolrDownloader("kw");
		sd.queryRecursive("文具");
		System.out.println(sd.kwArray);
	}

	public HashSet<String> tier2() {
		return tier2;
	}

	public void index(String indexName) {
		this.index = indexName;
	}

	public void startN(int startN) {
		this.startN = startN;
	}

	public void rows(int rows) {
		this.rows = rows;
	}

	public void queryRecursive(String queryString) {
		kwArray = new JSONArray();
		kwArray.add(query(queryString));
		for (int i = 0; i < docArray.size(); i++) {
			if (!kwArray.contains(docArray.getJSONObject(i))) {
				kwArray.add(query(docArray.getJSONObject(i).getString("kw")));
			}
		}
	}

	public JSONObject queryIndie(String queryString) {
		queryBase = "http://localhost:8983/solr/" + index + "/select?wt=json&rows=" + rows + "&start=" + startN + "&q=";
		try {
			String qs = URLEncoder.encode(queryString, "utf-8");// 精确搜索
			textResult = download(queryBase + qs).text();// 文本结果
			if (textResult != null)
				jsonResult = JSONObject.parseObject(textResult);// 解析为json
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		numFound = jsonResult.getJSONObject("response").getIntValue("numFound");// 获取查询结果计数

		JSONObject j = new JSONObject();
		j.put("kw", queryString);
		j.put("count", jsonResult.getJSONObject("response").getIntValue("numFound"));

		if (docArray == null)// 只进行一次循环
			docArray = jsonResult.getJSONObject("response").getJSONArray("docs");// 通过json获取实际doc结果字段
		for (int i = 0; i < docArray.size(); i++) {
			((JSONObject) docArray.get(i)).remove("_version_");
			((JSONObject) docArray.get(i)).remove("id");
		}

		return j;
	}

	public JSONObject query(String queryString) {
		queryBase = "http://eric1981.space/solr/" + index + "/select?wt=json&rows=" + rows + "&start=" + startN + "&q=";
		try {
			String qs = URLEncoder.encode(queryString, "utf-8");// 精确搜索
			textResult = download(queryBase + qs).text();// 文本结果
			if (textResult != null)
				jsonResult = JSONObject.parseObject(textResult);// 解析为json
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		numFound = jsonResult.getJSONObject("response").getIntValue("numFound");// 获取查询结果计数

		JSONObject j = new JSONObject();
		j.put("kw", queryString);
		j.put("count", jsonResult.getJSONObject("response").getIntValue("numFound"));

		if (docArray == null)// 只进行一次循环
			docArray = jsonResult.getJSONObject("response").getJSONArray("docs");// 通过json获取实际doc结果字段
		for (int i = 0; i < docArray.size(); i++) {
			((JSONObject) docArray.get(i)).remove("_version_");
			((JSONObject) docArray.get(i)).remove("id");
		}

		return j;
	}

	public HashSet<String> docValue(String key) {
		HashSet<String> docValue = new HashSet<>();
		for (int i = 0; i < docArray.size(); i++) {
			docValue.add(docArray.getJSONObject(i).getString(key));
		}
		return docValue;
	}

	public JSONArray kwArray() {
		return kwArray;
	}

	public int numFound() {
		return numFound;
	}

	public JSONArray docs() {
		return docArray;
	}

}
